﻿
#ifndef SERIALLINK_H
#define SERIALLINK_H


#include <QObject>
#include <QSerialPort>
#include <QSerialPortInfo>
#include <QMutex>
#include <QByteArray>

#include "Stdafx.h"
#include "Linkinterface.h"
#include "SerialConfig.h"
#include "controllers/SerialDebugger.h"

class SerialLink : public LinkInterface
{
    Q_OBJECT
    Q_PROPERTY_AUTO(int     , baud)
    Q_PROPERTY_AUTO(int     , dataBits)
    Q_PROPERTY_AUTO(int     , stopBits)
    Q_PROPERTY_AUTO(int     , parity)
    Q_PROPERTY_AUTO(QString , portName)
public:
    explicit SerialLink(const QString & port_name,
                        int baud = 9600,int data_bits = 8,
                        int stop_bits = 1,int parity= 0 ,QObject *parent = nullptr);
    virtual ~SerialLink();
    Q_INVOKABLE bool  connect      ()override;
    Q_INVOKABLE void  disConnect   ()override;
    Q_INVOKABLE bool  isConnected  ()override;
public slots:
    Q_INVOKABLE void writeBytes(QByteArray  data) override;
private slots:
    void readBytes_           (void);
private:
    QSerialPort*   port_ {};
    QMutex         dataMutex_;
    bool           isConnected_{false};
};

#endif // SERIALLINK_H
